/*
    Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "GameOverWidget.h"
#include "../Game.h"

GameOverWidget::GameOverWidget(Widget *parent /*= nullptr*/)
    : Widget(parent)
{
}

bool GameOverWidget::init()
{
    if (!Widget::init())
    {
        return false;
    }

    m_label = Label::create(this);
    m_homeButton = PushButton::create(this);
    auto cach = GameWorld::getInstance()->getDefultSpriteFrameCache();
    auto sf_normal = cach->getSpriteFrame(L"home-1");
    auto sf_pushed = cach->getSpriteFrame(L"home-2");
    m_homeButton->setBackgroundImage(sf_normal);
    m_homeButton->setBackgroundImage(sf_pushed, Rect::Zero, Widget::WidgetState::Pressed);

    m_homeButton->setClickedListener([](EventMouse *)
                                     {
		Game::getInstance()->playAudio(Game::AudioType::BUTTON_AUDIO);
		Game::getInstance()->resetGame(); });

    m_nextButton = PushButton::create(this);
    auto sf_next_nomal = cach->getSpriteFrame(L"continue-1");
    auto sf_next_push = cach->getSpriteFrame(L"continue-2");
    m_nextButton->setBackgroundImage(sf_next_nomal);
    m_nextButton->setBackgroundImage(sf_next_push, Rect::Zero, Widget::WidgetState::Pressed);
    m_nextButton->setClickedListener([](EventMouse *)
                                     {
		Game::getInstance()->playAudio(Game::AudioType::BUTTON_AUDIO);
		Game::getInstance()->nextGameLevelScene(); });

    m_label->setFont(Font(L"Segoe Script", 30, Font::FONT_WEIGHT_BOLD));
    m_label->setTextColor(Color4f::LightGoldenrodYellow);

    m_homeButton->hide();
    m_nextButton->hide();
    return true;
}

void GameOverWidget::layoutNextUi()
{
    m_label->resize(Size(400, 50));
    m_label->setPosition(Vector2(getSize().width / 2 - 200, 200));

    m_homeButton->resize(Size(100, 100));
    m_homeButton->setPosition(Vector2(getSize().width / 2 - 120, 60));

    m_nextButton->resize(Size(100, 100));
    m_nextButton->setPosition(Vector2(getSize().width / 2 + 20, 60));

    m_homeButton->show();
    m_nextButton->show();
}

void GameOverWidget::layoutHomeUi()
{
    m_label->resize(Size(400, 50));
    m_label->setPosition(Vector2(getSize().width / 2 - 200, 200));

    m_homeButton->resize(Size(100, 100));
    m_homeButton->setPosition(Vector2(getSize().width / 2 - 50, 60));
    m_homeButton->show();
}

GameOverWidget *GameOverWidget::create(Widget *parent /*= nullptr*/)
{
    auto p = makeWidgetPtr(parent);
    if (p && p->init())
    {
        if (p->getParent())
            p->getParent()->addChildren(p);
        return p;
    }
    return nullptr;
}

GameOverWidget::~GameOverWidget()
{
}

void GameOverWidget::setVictory()
{
    this->layoutHomeUi();
    m_label->setText(L"胜利");
}

void GameOverWidget::setDefeated()
{
    this->layoutHomeUi();
    m_label->setText(L"胜败乃兵家常事");
}

void GameOverWidget::setNextLevel()
{
    this->layoutNextUi();
    m_label->setText(L"通关成功，可继续下一关");
}

int GameOverWidget::getDefultWidth() const
{
    return 500;
}

int GameOverWidget::getDefultHeight() const
{
    return 300;
}

void GameOverWidget::draw(IRender *render, const Matrix4X4 &transform)
{
    DirectX::XMMATRIX mvp = DirectX::XMMatrixMultiply(transform, getCamera()->getViewProjectMat());
    if (m_currentBgImage)
    {
        m_currentBgImage->draw(render, mvp);
    }
}

void GameOverWidget::onWidgetStateChange()
{
    //只有一种状态，

    m_currentBgImage = getStyleInforByState(Widget::WidgetState::Normal)->backgroundImage.get();
}

void GameOverWidget::onWidgetStyleChange()
{
    //只有一种状态，

    m_currentBgImage = getStyleInforByState(Widget::WidgetState::Normal)->backgroundImage.get();
}
